package ynu.edu;

import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4JCircuitBreakerFactory;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
import org.springframework.cloud.client.circuitbreaker.Customizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class FaultToleranceConfig {

    /**
     * 配置默认熔断器
     */
    @Bean
    public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
        return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
                .circuitBreakerConfig(CircuitBreakerConfig.custom()
                        // 滑动窗口大小
                        .slidingWindowSize(10)
                        // 失败率阈值（50%）
                        .failureRateThreshold(50)
                        // 等待时间（5秒）
                        .waitDurationInOpenState(Duration.ofSeconds(5))
                        // 半开状态下的最大请求数
                        .permittedNumberOfCallsInHalfOpenState(3)
                        // 慢调用阈值（2秒）
                        .slowCallDurationThreshold(Duration.ofSeconds(2))
                        // 慢调用失败率阈值（50%）
                        .slowCallRateThreshold(50)
                        .build())
                .timeLimiterConfig(TimeLimiterConfig.custom()
                        // 超时时间（3秒）
                        .timeoutDuration(Duration.ofSeconds(3))
                        .build())
                .build());
    }

    /**
     * 配置用户服务的熔断器
     */
    @Bean
    public Customizer<ReactiveResilience4JCircuitBreakerFactory> userServiceCustomizer() {
        return factory -> factory.configure(builder -> builder
                .circuitBreakerConfig(CircuitBreakerConfig.custom()
                        .slidingWindowSize(5)
                        .failureRateThreshold(60)
                        .waitDurationInOpenState(Duration.ofSeconds(10))
                        .permittedNumberOfCallsInHalfOpenState(2)
                        .slowCallDurationThreshold(Duration.ofSeconds(1))
                        .slowCallRateThreshold(60)
                        .build())
                .timeLimiterConfig(TimeLimiterConfig.custom()
                        .timeoutDuration(Duration.ofSeconds(2))
                        .build()), "userService");
    }

    /**
     * 配置购物车服务的熔断器
     */
    @Bean
    public Customizer<ReactiveResilience4JCircuitBreakerFactory> cartServiceCustomizer() {
        return factory -> factory.configure(builder -> builder
                .circuitBreakerConfig(CircuitBreakerConfig.custom()
                        .slidingWindowSize(8)
                        .failureRateThreshold(40)
                        .waitDurationInOpenState(Duration.ofSeconds(8))
                        .permittedNumberOfCallsInHalfOpenState(3)
                        .slowCallDurationThreshold(Duration.ofSeconds(1))
                        .slowCallRateThreshold(50)
                        .build())
                .timeLimiterConfig(TimeLimiterConfig.custom()
                        .timeoutDuration(Duration.ofSeconds(2))
                        .build()), "cartService");
    }
} 